package org.cuatrovientos.englishquiz;

import java.util.Scanner;
import java.util.Vector;

/**
 * class Quiz
 * @author dani
 *
 */

public class Quiz {
	private Vector<Question> questions;
	private int correctAnswer;
	private String quizName;
	
	
	/**
	 * 
	 * @param name
	 */
	public Quiz (String name) {
		
		this.setQuestions (new Vector<Question>());
		this.correctAnswer = 0;
		this.setQuizName(name);
	}
	
	/**
	 * inits vector with questions
	 */
	public void addQuestions () {
		getQuestions().add(new Question("We study __ the university","in","on","at","into","at"));
		getQuestions().add(new Question("We study __ the school","in","on","at","into","at"));
		getQuestions().add(new Question("Tyrion is __ prison","in","on","at","into","in"));
		getQuestions().add(new Question("Tyson was  __ jail","in","on","at","into","in"));
		getQuestions().add(new Question("I was  __ the beach","in","on","at","into","on"));
		getQuestions().add(new Question("Turn __ the left","in","on","at","into","on"));
		getQuestions().add(new Question("I was waiting __ the corner","in","on","at","into","at"));
		getQuestions().add(new Question("We met __ the theatre","in","on","at","into","at"));
		getQuestions().add(new Question("It depends  __ the weather","in","on","at","off","in"));
		getQuestions().add(new Question("We went  __ train","in","on","onto","by","by"));
	}
	
	/**
	 * add new question
	 */
	public void addNewQuestion (Question myQuestion) {
		getQuestions().add(new Question(myQuestion.getQuestioName(), myQuestion.getOption1(), myQuestion.getOption2(),
				myQuestion.getOption3(), myQuestion.getOption4(), myQuestion.getSolution()));
	}
	
	/**
	 * delete question
	 * @param questionName
	 */
	public void deleteQuestion (String questionName) {
		for (int i = 0; i<getQuestions().size() -1; i++){
			if (questionName == getQuestions().elementAt(i).getQuestioName()) {
				getQuestions().removeElementAt(i);
			}
		}
	}
	
	
	/**
	 * retur the size
	 * @return
	 */
	public int quizLength () {
		return getQuestions().size();
	}

	
	public void makeQuiz () {
		Scanner reader = new Scanner(System.in);
		String line = "";
		int numberQuestion = 1;
		
		for (Question q :this.questions) {
			System.out.println("Question " + numberQuestion + ": " + q.getQuestioName());
			System.out.println("Options ");
			System.out.println(q.getOption1());
			System.out.println(q.getOption2());
			System.out.println(q.getOption3());
			System.out.println(q.getOption4());
			System.out.print("Write the correct: ");
			line = reader.nextLine();
			
			if (line.toLowerCase().equals(q.getSolution())) {
				correctAnswer++;
				System.out.println("Ok, That was correct");
				System.out.println("Correct answers: " + correctAnswer + " out of " + 10);
			} else {
				System.out.println("Wrong answer. The correct was: ");
				System.out.println(q.getSolution());
				System.out.println("Correct answers: " + correctAnswer + " out of " + 10);
			}
			
			numberQuestion++;
		}
		reader.close();
		}
	
	
		/**
		 * Method to check the right answers
		 * @return
		 */
		public String checkResult() {
			if (correctAnswer >= (quizLength()/2)) {
				return "Congratulations, you passed the exam with " + correctAnswer + " correct answers";
			} else {
				return "You failed, you only answered " + correctAnswer + " correct answers. Better luck next time";			
			}
		}

		public Vector <Question> getQuestions() {
			return questions;
		}

		public void setQuestions(Vector <Question> questions) {
			this.questions = questions;
		}

		public String getQuizName() {
			return quizName;
		}

		public void setQuizName(String quizName) {
			this.quizName = quizName;
		}
	}
